package com.hsoft.zhxy.app.xtgl_02_01.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsoft.zhxy.app.common.model.entity.YhglEntity;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.YhglMapper;
import com.hsoft.zhxy.app.xtgl_02_01.model.query.YhglQuery;
import com.hsoft.zhxy.app.xtgl_02_01.model.vo.YhjcxxVO;
import com.hsoft.zhxy.app.xtgl_02_01.service.YhglService;
import com.hsoft.zhxy.common.base.BaseService;
import com.hsoft.zhxy.common.exception.BizException;
import com.hsoft.zhxy.common.exception.ExcpCodeInfoEnum;
import com.hsoft.zhxy.common.result.ResultBody;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Log4j2
@Service
public class YhglServiceImpl extends BaseService implements YhglService {

    @Autowired
    private YhglMapper yhglMapper;

    /**
     * 查询单条数据
     *
     * @param yhglEntity
     * @return
     */
    public JSONObject findOne(YhglEntity yhglEntity) {
        YhglEntity rs = yhglMapper.selectOne(new QueryWrapper<YhglEntity>().setEntity(yhglEntity));
        return ResultBody.setData(rs);
    }

    /**
     * 查询列表数据
     *
     * @param yhglEntity
     * @return
     */
    public JSONObject findList(YhglEntity yhglEntity) {
        List<YhglEntity> list = yhglMapper.selectList(new QueryWrapper<YhglEntity>().setEntity(yhglEntity));
        return ResultBody.setData(list);
    }

    /**
     * 查询分页数据
     *
     * @param yhglEntity
     * @param page
     * @param pageSize
     * @return
     */
    public JSONObject findListByPage(YhglEntity yhglEntity, long page, long pageSize) {

        Page<YhglEntity> pageObj = new Page<YhglEntity>(page, pageSize);
        IPage<YhglEntity> result = yhglMapper.selectPage(pageObj, new QueryWrapper<YhglEntity>().setEntity(yhglEntity));

        return ResultBody.setData(result.getTotal(), result.getRecords());
    }

    /**
     * 新增
     *
     * @param yhglEntity
     * @return
     */
    public JSONObject add(YhglEntity yhglEntity) {
        yhglEntity.setCjsj(new Date());
        yhglEntity.setCjr(getYhid());
        yhglEntity.setBgsj(new Date());
        yhglEntity.setBgr(getYhid());
        return ResultBody.setData(yhglMapper.insert(yhglEntity));
    }

    /**
     * 修改
     *
     * @param yhglEntity
     * @return
     */
    public JSONObject update(YhglEntity yhglEntity) {
        yhglEntity.setBgsj(new Date());
        yhglEntity.setBgr(getYhid());
        return ResultBody.setData(yhglMapper.updateById(yhglEntity));
    }

    /**
     * 删除
     *
     * @param yhglEntity
     * @return
     */
    public JSONObject delete(YhglEntity yhglEntity) {
        return ResultBody.setData(yhglMapper.delete(new QueryWrapper<YhglEntity>().setEntity(yhglEntity)));
    }

    /**
     * 获取用户信息和部门信息
     *
     * @param yhglQuery
     * @return
     */
    @Override
    public JSONObject findListOfYhxxAndYhbmByPage(YhglQuery yhglQuery) {
        Page<YhglQuery> pageObj = new Page<YhglQuery>(yhglQuery.getPage(), yhglQuery.getPageSize());
        QueryWrapper<YhglQuery> queryWrapper = new QueryWrapper<>();
//
//        if (StringUtils.isBlank(yhglQuery.getYhid())) {
//            throw new BizException(ExcpCodeInfoEnum.USER_ERROR_A0151);
//        }
//
//        queryWrapper.like(!StringUtils.isBlank(yhglQuery.getYhid()), "yh.yhid", yhglQuery.getYhid())
//                .like(!StringUtils.isBlank(yhglQuery.getXm()), "yh.xm", yhglQuery.getXm())
//                .eq(yhglQuery.getYhzt() != null, "yh.yhzt", yhglQuery.getYhzt())
//                .orderByAsc("cjsj");

        IPage<YhjcxxVO> result = yhglMapper.listYhxxAndBmByYhid(pageObj, queryWrapper);
        return ResultBody.setData(result.getTotal(), result.getRecords());
    }

}
